<?php
class User{
	 var $db_conn;
	 public $userName;
	 public $password;
	 public $sex;
	 public $level;
	 public $exp;
	 public $phone;
	 public $email;
	 public $deliver_addr;
	 //public $other_db;
	 
	 function __construct(){
	 	   //$this->other_db=new DB();
	 	   
	 	    $this->connect_db();
	 	 
		    mysql_query("set names utf8");
	 }
	 function __destruct(){
	 
	 	//echo '<script language="JavaScript">alert("Destructing");<script/>';
	    $this->close_db();
	 }
	 function connect_db(){
	 
	 	$database_username="root";
		$database_psaaword="";
		$database_name="eshop";
		$this->db_conn = mysql_connect("localhost", $database_username, $database_psaaword)or die ("Could not connect:".mysql_error());
		mysql_select_db($database_name,$this->db_conn)or die ("can not use database:".mysql_error());
		//echo $this->db_conn;
	 }
	 function close_db(){
	 
	    if($this->db_conn!="")
			{
			mysql_close($this->db_conn)or die ("ERROR:".mysql_error());
			}
	 }
	 
	
	 function update_Pwd($newPwd){
	 
	 	$sql="UPDATE user Set password='".$newPwd."' WHERE userName='".$this->userName."'";
		mysql_query($sql,$this->db_conn) or die("ERROR:".mysql_error());
		$this->password=$newPwd;
		
	 }
	
	 function update_userName($newName){	 	
	 	    $sql="UPDATE user Set userName='".$newName."' WHERE userName='".$this->userName."'";
		    mysql_query($sql,$this->db_conn)or die("ERROR:".mysql_error());
		    $this->userName=$newName;
	 }
	 

	 function load_info($userName){
	 	$sql="Select * from users Where userName='".$userName."'";
		$result=mysql_query($sql,$this->db_conn);
		$this->userName=$userName;
		$this->password=mysql_result($result, 0,"password");
		$this->level=mysql_result($result, 0,"level");
		$this->sex=mysql_result($result, 0,"sex");
		$this->exp=mysql_result($result, 0,"exp");
		$this->phone=mysql_result($result, 0,"phone");
		$this->email=mysql_result($result, 0,"email");
		$this->deliver_addr=mysql_result($result, 0,"deliver_addr");
	 }
	 
	
	 function delete_self(){
	 	$sql = "DELETE FROM user WHERE userName='".$this->userName."'";
		mysql_query($sql,$this->db_conn)or die("ERROR:".mysql_error());
	 }
	 
	
	 function insert_self(){
	 	$sql = "INSERT INTO user(
			userName,
			password,
			level,
			sex,
			exp,
			phone,
			email,
			deliver_addr) 
			VALUES('" 
			. $this->userName . "', '" 
			. $this->password . "', " 
			. $this->level .", " 
			. $this->sex .", "
			. $this->exp .", "
			. $this->phone .", '"
			. $this->email."', '" 
			. $this->deliver_addr ."')";
			mysql_query($sql,$this->db_conn)or die("ERROR:".mysql_error());
	 }
	 
	
	 function update_self(){
	 	
		$sql="UPDATE user Set sex=".$this->sex." WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR1:".mysql_error());
		$sql="UPDATE user Set phone=".$this->phone." WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR2:".mysql_error());
		$sql="UPDATE user Set email='".$this->email."' WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR3:".mysql_error());
		$sql="UPDATE user Set deliver_addr='".$this->deliver_addr."' WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR4:".mysql_error());
		$sql="UPDATE user Set exp=".$this->exp." WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR5:".mysql_error());
		$this->level=$this->exp/100;
		$sql="UPDATE user Set level=".$this->level." WHERE userName='" . $this->userName . "'";
		mysql_query($sql,$this->db_conn)or die("ERROR6:".mysql_error());		
	 }

}
?>